//
//  AuditDetailViewController.m
//  auditorias
//
//  Created by Mauro on 7/30/13.
//  Copyright (c) 2013 Antilope. All rights reserved.
//

#import "AuditDetailViewController.h"
#import "SharedMemory.h"
#import "ControlVariables.h"
#import "PointOfSell.h"
#import "User.h"
#import "Comment.h"
#import "AuditType.h"

@interface AuditDetailViewController ()

@end

@implementation AuditDetailViewController

@synthesize items;
@synthesize table;
@synthesize currentSelection;
@synthesize pointOfSell;

- (void) dealloc
{
    [items release], items = nil ;
    [table release], table = nil;
    [pointOfSell release], pointOfSell = nil;
    [_topBarView release];
    [super dealloc];
}

- (void)viewDidUnload
{
    self.items = nil;
    self.table = nil;
    self.pointOfSell = nil;
    [super viewDidUnload];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [TopBarViewController setTitleLabelOf:_topBarView text:kTopBarTitleAuditDetail];
    
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    SharedMemory *mem = [SharedMemory sharedInstance];
    self.pointOfSell = [mem getPointOfSell];
    
    UILabel *titleLabel = (UILabel *)[self.view viewWithTag:30];
    [titleLabel setText:self.pointOfSell.name];
    
    self.items = [[[NSMutableArray alloc] initWithArray:self.pointOfSell.comments] autorelease];
    
    UILabel *noCommentsView = (UILabel *)[self.view viewWithTag:31];
    noCommentsView.hidden = [self.items count]!=0;
    
    
    [self loadTable];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView: (UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
    return [items count];
}

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    //SharedMemory *mem = [SharedMemory sharedInstance];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AuditDetailCell"];
    Comment* m = [self.pointOfSell.comments objectAtIndex:[indexPath row] ];
    AuditType* auditType = [SharedMemory findAuditTypeByExternalId:m.auditTypeId];
    
    UILabel *textViewComm = (UILabel *)[cell viewWithTag:40];
    UILabel *textViewUser = (UILabel *)[cell viewWithTag:41];
    UILabel *textViewDate = (UILabel *)[cell viewWithTag:42];
    UILabel *textViewEvMe = (UILabel *)[cell viewWithTag:43];
    UILabel *textViewEvaNa = (UILabel *)[cell viewWithTag:44];
    
    [textViewComm setText: (([m.message isEqualToString:@""])? @"(sin comentario)" : m.message)];
    if (auditType==nil) {
        [textViewUser setText:m.userName];
    }
    else{
        NSString *auditTypeText = [NSString stringWithFormat:@"%@ - ",auditType.name];
        NSString *fullText = [NSString stringWithFormat:@"%@%@",auditTypeText,m.userName];
        [textViewUser setText:fullText];
        NSMutableAttributedString *text = [[[NSMutableAttributedString alloc] initWithAttributedString: textViewUser.attributedText] autorelease];
        [text addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue" size:14.0] range:NSMakeRange(0, fullText.length)];
        [text addAttribute: NSForegroundColorAttributeName value: [UIColor lightGrayColor] range: NSMakeRange(0,fullText.length)];
        [text addAttribute: NSForegroundColorAttributeName value: [UIColor whiteColor] range: NSMakeRange(0, auditTypeText.length)];
        NSMutableParagraphStyle *paragrahStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
        [paragrahStyle setAlignment:NSTextAlignmentRight];
        [text addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, fullText.length)];
        [textViewUser setAttributedText: text];
    }
    [textViewDate setText:m.date];
    [textViewEvMe setText:m.evaluatorMessage];
    
    int heightOfComment = [AuditDetailViewController heightOfComment:m.message];
    int heightOfEvComment = [AuditDetailViewController heightOfEvaluatorComment:m.evaluatorMessage];
    //int heightOfComment = [textViewComment.con];
    textViewComm.frame = CGRectMake(textViewComm.frame.origin.x, 5 , textViewComm.frame.size.width, heightOfComment);
    textViewUser.frame = CGRectMake(textViewUser.frame.origin.x, 0 + textViewComm.frame.origin.y + textViewComm.frame.size.height, textViewUser.frame.size.width, textViewUser.frame.size.height);
    textViewDate.frame = CGRectMake(textViewDate.frame.origin.x, -5 + textViewUser.frame.origin.y + textViewUser.frame.size.height, textViewDate.frame.size.width, textViewDate.frame.size.height);
    textViewEvMe.frame = CGRectMake(textViewEvMe.frame.origin.x, 8 + textViewDate.frame.origin.y + textViewDate.frame.size.height, textViewEvMe.frame.size.width, heightOfEvComment);
    textViewEvaNa.frame = CGRectMake(textViewEvaNa.frame.origin.x, 0 + textViewEvMe.frame.origin.y + textViewEvMe.frame.size.height, textViewEvaNa.frame.size.width, textViewEvaNa.frame.size.height);
    
    if ([m.evaluatorMessage isEqualToString:@""]) {
        textViewEvMe.hidden = true;
        textViewEvaNa.hidden = true;
    }
    else{
        textViewEvMe.hidden = false;
        textViewEvaNa.hidden = false;
    }
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Comment* m = [self.pointOfSell.comments objectAtIndex:[indexPath row] ];
    int heightOfUserComment = [AuditDetailViewController heightOfComment:m.message] + 5 + 21 + 0 + 21 - 5 + 5;
    if ([m.evaluatorMessage isEqualToString:@""])
        return heightOfUserComment;
    return heightOfUserComment + [AuditDetailViewController heightOfEvaluatorComment:m.evaluatorMessage] + 8 + 21 + 0;
}

+ (CGFloat) heightOfComment: (NSString *)content
{
    CGFloat contentHeight =
    [content sizeWithFont: [UIFont boldSystemFontOfSize:15]
        constrainedToSize: CGSizeMake( 269, 21 * 3 )
            lineBreakMode: NSLineBreakByTruncatingTail].height;
    return contentHeight + 0;
}

+ (CGFloat) heightOfEvaluatorComment: (NSString *)content
{
    CGFloat contentHeight =
    [content sizeWithFont: [UIFont italicSystemFontOfSize:15]
        constrainedToSize: CGSizeMake( 269, 21 * 3 )
            lineBreakMode: NSLineBreakByTruncatingTail].height;
    return contentHeight + 0;
}

- (IBAction)finishButtonTapped:(id)sender
{
    [self.tabBarController setSelectedIndex:2];
}

- (IBAction)previousButtonTapped:(id)sender
{
    [self.tabBarController setSelectedIndex:0];
}

- (void) loadTable
{
    [self.table reloadData];
}


@end
